package com.farm.forms.action;

import com.farm.forms.domain.Title;
import com.farm.forms.service.TitleServiceInter;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBSort;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.web.easyui.EasyUiUtils;
import java.util.Map;
import com.farm.core.page.CommitType;
import com.farm.core.page.PageSet;
import com.farm.core.page.PageType;
import com.farm.web.WebSupport;
import com.farm.web.spring.BeanFactory;

/* *
 *功能：结果集合控制层
 *详细：
 *
 *版本：v0.1
 *作者：王东
 *日期：20141124152033
 *说明：
 */
public class TitleAction extends WebSupport {
	private Map<String, Object> jsonResult;// 结果集合
	private DataQuery query;// 条件查询
	private Title entity;// 实体封装
	private PageSet pageset;// 请求状态
	private String ids;// 主键集合

	/**
	 * 查询结果集合
	 * 
	 * @return
	 */
	public String queryall() {
		try {
			query = EasyUiUtils.formatGridQuery(getRequest(), query);
			if (ids == null || ids.trim().length() <= 0) {
				ids = "NONE";
			}
			query.addRule(new DBRule("QUERYID", ids, "="));
			query.setDefaultSort(new DBSort("SORT", "ASC"));
			DataResult result = titleIMP.createTitleSimpleQuery(query).search();
			result.runDictionary("1:是,0:否", "FIXIS");
			result.runDictionary("0:无,1:枚举,2:数据字典,3:日期", "DICTYPE");
			jsonResult = EasyUiUtils.formatGridData(result);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	/**
	 * 提交修改数据
	 * 
	 * @return
	 */
	public String editSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			entity = titleIMP.editTitleEntity(entity, getCurrentUser());
			pageset = new PageSet(PageType.UPDATE, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.UPDATE,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 提交新增数据
	 * 
	 * @return
	 */
	public String addSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			entity = titleIMP.insertTitleEntity(entity, getCurrentUser());
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.ADD,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 自动生成结果集合且删除现有集合
	 * 
	 * @return
	 */
	public String initTitles() {
		try {
			titleIMP.initTitles(ids, getCurrentUser());
			pageset = new PageSet(PageType.OTHER, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.OTHER,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 删除数据
	 * 
	 * @return
	 */
	public String delSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			for (String id : parseIds(ids)) {
				titleIMP.deleteTitleEntity(id, getCurrentUser());
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 跳转
	 * 
	 * @return
	 */
	public String forSend() {
		// TODO 自动生成代码,修改后请去除本注释
		return SUCCESS;
	}

	/**
	 * 显示详细信息（修改或浏览时）
	 * 
	 * @return
	 */
	public String view() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			switch (pageset.getPageType()) {
			case (1): {// 新增
				return SUCCESS;
			}
			case (0): {// 展示
				entity = titleIMP.getTitleEntity(ids);
				return SUCCESS;
			}
			case (2): {// 修改
				entity = titleIMP.getTitleEntity(ids);
				return SUCCESS;
			}
			default:
				break;
			}
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.OTHER,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	private final static TitleServiceInter titleIMP = (TitleServiceInter) BeanFactory
			.getBean("farm_forms_title_ProxyId");

	// ----------------------------------------------------------------------------------
	public DataQuery getQuery() {
		return query;
	}

	public void setQuery(DataQuery query) {
		this.query = query;
	}

	public Title getEntity() {
		return entity;
	}

	public void setEntity(Title entity) {
		this.entity = entity;
	}

	public PageSet getPageset() {
		return pageset;
	}

	public void setPageset(PageSet pageset) {
		this.pageset = pageset;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Map<String, Object> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, Object> jsonResult) {
		this.jsonResult = jsonResult;
	}

	private static final long serialVersionUID = 1L;
}
